<?php

namespace app\admin\controller;

use app\admin\model\Wxapp as WxappModel;

/**
 * 商户管理控制器
 */
class Wxapp extends Controller
{
    /**
     * 商户列表
     */
    public function index()
    {
        $model = new WxappModel;
        $list = $model->getAllList();
        return $this->fetch('index', compact('list'));
    }

    /**
     * 添加
     */
    public function add()
    {
        if (!$this->request->isAjax()) {
            return $this->fetch('add');
        }
		$shop = $this->postData('shop');
		$model = new StoreUserModel;
		if($model->getUserName($shop['user_name'])){
			$error = $model->getError() ?: '商户账户已存在';
			return $this->renderError($error);
		}
        $model = new WxappModel;
        if ($model->add($shop)) {
            return $this->renderSuccess('添加成功', url('shop/index'));
        }
        $error = $model->getError() ?: '添加失败';
        return $this->renderError($error);
    }

    /**
     * 删除
     */
    public function delete($wxapp_id)
    {
		return $this->renderError('暂不支持删除');
        $model = WxappModel::getWxapp($wxapp_id);
        if (!$model->remove()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 编辑
     */
    public function edit($wxapp_id)
    {
        // 详情
        $model = WxappModel::getWxapp(['wxapp_id' => $wxapp_id]);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        // 更新记录
        if ($model->edit($this->postData('shop'))) {
            return $this->renderSuccess('更新成功', url('shop/index'));
        }
        $error = $model->getError() ?: '更新失败';
        return $this->renderError($error);
    }

}
